package cn.ht.jywrwater.module.ht.controller.admin.customermeter;

import cn.ht.jywrwater.framework.common.pojo.CommonResult;
import cn.ht.jywrwater.framework.common.pojo.PageResult;
import cn.ht.jywrwater.framework.excel.core.util.ExcelUtils;
import cn.ht.jywrwater.framework.operatelog.core.annotations.OperateLog;
import cn.ht.jywrwater.module.ht.controller.admin.customermeter.vo.*;
import cn.ht.jywrwater.module.ht.convert.customermeter.CustomerMeterConvert;
import cn.ht.jywrwater.module.ht.dal.dataobject.customermeter.CustomerMeterDO;
import cn.ht.jywrwater.module.ht.service.customermeter.CustomerMeterService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static cn.ht.jywrwater.framework.common.pojo.CommonResult.success;
import static cn.ht.jywrwater.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 水表用户绑定关系")
@RestController
@RequestMapping("/ht/customer-meter")
@Validated
public class CustomerMeterController {

    @Resource
    private CustomerMeterService customerMeterService;

    @PostMapping("/create")
    @Operation(summary = "创建水表用户绑定关系")
    @PreAuthorize("@ss.hasPermission('ht:customer-meter:create')")
    public CommonResult<Long> createCustomerMeter(@Valid @RequestBody CustomerMeterCreateReqVO createReqVO) {
        return success(customerMeterService.createCustomerMeter(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新水表用户绑定关系")
    @PreAuthorize("@ss.hasPermission('ht:customer-meter:update')")
    public CommonResult<Boolean> updateCustomerMeter(@Valid @RequestBody CustomerMeterUpdateReqVO updateReqVO) {
        customerMeterService.updateCustomerMeter(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除水表用户绑定关系")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('ht:customer-meter:delete')")
    public CommonResult<Boolean> deleteCustomerMeter(@RequestParam("id") Long id) {
        customerMeterService.deleteCustomerMeter(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得水表用户绑定关系")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('ht:customer-meter:query')")
    public CommonResult<CustomerMeterRespVO> getCustomerMeter(@RequestParam("id") Long id) {
        CustomerMeterDO customerMeter = customerMeterService.getCustomerMeter(id);
        return success(CustomerMeterConvert.INSTANCE.convert(customerMeter));
    }

    @GetMapping("/list")
    @Operation(summary = "获得水表用户绑定关系列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('ht:customer-meter:query')")
    public CommonResult<List<CustomerMeterRespVO>> getCustomerMeterList(@RequestParam("ids") Collection<Long> ids) {
        List<CustomerMeterDO> list = customerMeterService.getCustomerMeterList(ids);
        return success(CustomerMeterConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得水表用户绑定关系分页")
    @PreAuthorize("@ss.hasPermission('ht:customer-meter:query')")
    public CommonResult<PageResult<CustomerMeterRespVO>> getCustomerMeterPage(@Valid CustomerMeterPageReqVO pageVO) {
        PageResult<CustomerMeterDO> pageResult = customerMeterService.getCustomerMeterPage(pageVO);
        return success(CustomerMeterConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出水表用户绑定关系 Excel")
    @PreAuthorize("@ss.hasPermission('ht:customer-meter:export')")
    @OperateLog(type = EXPORT)
    public void exportCustomerMeterExcel(@Valid CustomerMeterExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<CustomerMeterDO> list = customerMeterService.getCustomerMeterList(exportReqVO);
        // 导出 Excel
        List<CustomerMeterExcelVO> datas = CustomerMeterConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "水表用户绑定关系.xls", "数据", CustomerMeterExcelVO.class, datas);
    }

}
